local root "G:\My Drive\Mathematica files\BC23-TBD (SSI Medicaid)\Data\Output\"

local grph_combine_overall
local grph_combine_nottrained

foreach st in AR CO LA MA {

import excel using "`root'\Analytic File\2019\graph_data_2019_20230703.xlsx", sheet("`st'_2019") cellrange("A5") first clear
destring overall_* trained_* not_*, replace
drop if Obs == .
count
local maxobs = r(N)
local newobs = 20 - `maxobs' +1
expand `newobs' if Obs == `maxobs'
ds Obs P_SSI_ID1_RANGE, not 
local varnames = r(varlist)
foreach var of local varnames {
	replace `var' = . if _n ~= Obs
} 
replace Obs = _n
gen bucket_min = (Obs-1)*5
gen bucket_max = Obs*5
tostring bucket_min bucket_max, force replace
gen newlabel = bucket_min + "-" + bucket_max + "%"

gen line = Obs * 0.05 - 0.025

forvalues i = 1/`=_N' {
 label define lab2   `=Obs[`i']'    "`=newlabel[`i']'", add
}
label values Obs lab2
label variable Obs " "
label variable overall_pct_on_ssi "Percentage on SSI"
label variable not_trained_pct_on_ssi "Percentage on SSI"

levelsof Obs if mod(Obs,2) == 1, local(Obsvalues)
graph twoway sc overall_pct_on_ssi Obs, xlabel(`Obsvalues', valuelabels labsize(vsmall) labels) scheme(s1mono) xtitle("Predicted SSI probability") ytitle("Share receiving SSI") || li line Obs, lpattern(dash) legend(off) title("`st'") xsize(6)

*graph bar overall_pct_not_on_ssi overall_pct_on_ssi, over(newlabel, sort(Obs) relabel(1 "0-5%" 2 "10-15%" 3 " " 4 "20-25%" 5 " " 6 "30-35%" 7 " " 8 "40-45%" 9 " " 10 " " 11 "50-55%" 12 " " 13 "60-65%" 14 " " 15 "70-75%" 16 " " 17 "80-85%" 18 " " 19 "90-95%" 20 " ") label(labsize(vsmall))) stack scheme(s1mono) title("`st'") legend(order(1 "Not on SSI" 2 "On SSI")) xsize(6)
graph save "`root'/summary_graphs/graphs/`st'_overall_pctssi.gph", replace

local grph_combine_overall "`grph_combine_overall' `"`root'/summary_graphs/graphs/`st'_overall_pctssi"'"

levelsof Obs if mod(Obs,2) == 1, local(Obsvalues)
graph twoway sc not_trained_pct_on_ssi Obs, xlabel(`Obsvalues', valuelabels labsize(vsmall) labels) scheme(s1mono) xtitle("Predicted SSI probability") ytitle("Share receiving SSI") || li line Obs, lpattern(dash) legend(off) title("`st'") xsize(6)
*graph bar not_trained_pct_not_on_ssi not_trained_pct_on_ssi, over(newlabel, sort(Obs) relabel(1 "0-5%" 2 "10-15%" 3 " " 4 "20-25%" 5 " " 6 "30-35%" 7 " " 8 "40-45%" 9 " " 10 " " 11 "50-55%" 12 " " 13 "60-65%" 14 " " 15 "70-75%" 16 " " 17 "80-85%" 18 " " 19 "90-95%" 20 " ") label(labsize(vsmall))) stack scheme(s1mono) title("`st'") legend(order(1 "Not on SSI" 2 "On SSI")) xsize(6)
graph save "`root'/summary_graphs/graphs/`st'_nottrained_pctssi.gph", replace

local grph_combine_nottrained "`grph_combine_nottrained' `"`root'/summary_graphs/graphs/`st'_nottrained_pctssi"'"

}

graph combine `grph_combine_overall', rows(2) cols(2) scheme(s1mono) iscale(0.75) xsize(9) ysize(6)
graph export "`root'/summary_graphs/overall_pctssi.png", replace hei(1200)

graph combine `grph_combine_nottrained', rows(2) cols(2) scheme(s1mono) iscale(0.75) xsize(9) ysize(6)
graph export "`root'/summary_graphs/nottrained_pctssi.png", replace hei(1200)

local grph_combine_overall
local grph_combine_nottrained

foreach st in CA NY PA TX {

import excel using "`root'\Analytic File\2019\graph_data_2019_20230703.xlsx", sheet("`st'_2019") cellrange("A5") first clear
destring overall_* trained_* not_*, replace
drop if Obs == .
count
local maxobs = r(N)
local newobs = 20 - `maxobs' +1
expand `newobs' if Obs == `maxobs'
ds Obs P_SSI_ID1_RANGE, not 
local varnames = r(varlist)
foreach var of local varnames {
	replace `var' = . if _n ~= Obs
} 
replace Obs = _n
gen bucket_min = (Obs-1)*5
gen bucket_max = Obs*5
tostring bucket_min bucket_max, force replace
gen newlabel = bucket_min + "-" + bucket_max + "%"

gen line = Obs * 0.05 - 0.025

forvalues i = 1/`=_N' {
 label define lab2   `=Obs[`i']'    "`=newlabel[`i']'", add
}
label values Obs lab2
label variable Obs " "
label variable overall_pct_on_ssi "Percentage on SSI"
label variable not_trained_pct_on_ssi "Percentage on SSI"

levelsof Obs if mod(Obs,2) == 1, local(Obsvalues)
graph twoway sc overall_pct_on_ssi Obs, xlabel(`Obsvalues', valuelabels labsize(vsmall) labels) scheme(s1mono) xtitle("Predicted SSI probability") ytitle("Share receiving SSI") || li line Obs, lpattern(dash) legend(off) title("`st'") xsize(6)

*graph bar overall_pct_not_on_ssi overall_pct_on_ssi, over(newlabel, sort(Obs) relabel(1 "0-5%" 2 "10-15%" 3 " " 4 "20-25%" 5 " " 6 "30-35%" 7 " " 8 "40-45%" 9 " " 10 " " 11 "50-55%" 12 " " 13 "60-65%" 14 " " 15 "70-75%" 16 " " 17 "80-85%" 18 " " 19 "90-95%" 20 " ") label(labsize(vsmall))) stack scheme(s1mono) title("`st'") legend(order(1 "Not on SSI" 2 "On SSI")) xsize(6)
graph save "`root'/summary_graphs/graphs/`st'_overall_pctssi.gph", replace

local grph_combine_overall "`grph_combine_overall' `"`root'/summary_graphs/graphs/`st'_overall_pctssi"'"

levelsof Obs if mod(Obs,2) == 1, local(Obsvalues)
graph twoway sc not_trained_pct_on_ssi Obs, xlabel(`Obsvalues', valuelabels labsize(vsmall) labels) scheme(s1mono) xtitle("Predicted SSI probability") ytitle("Share receiving SSI") || li line Obs, lpattern(dash) legend(off) title("`st'") xsize(6)
*graph bar not_trained_pct_not_on_ssi not_trained_pct_on_ssi, over(newlabel, sort(Obs) relabel(1 "0-5%" 2 "10-15%" 3 " " 4 "20-25%" 5 " " 6 "30-35%" 7 " " 8 "40-45%" 9 " " 10 " " 11 "50-55%" 12 " " 13 "60-65%" 14 " " 15 "70-75%" 16 " " 17 "80-85%" 18 " " 19 "90-95%" 20 " ") label(labsize(vsmall))) stack scheme(s1mono) title("`st'") legend(order(1 "Not on SSI" 2 "On SSI")) xsize(6)
graph save "`root'/summary_graphs/graphs/`st'_nottrained_pctssi.gph", replace

local grph_combine_nottrained "`grph_combine_nottrained' `"`root'/summary_graphs/graphs/`st'_nottrained_pctssi"'"

}

graph combine `grph_combine_overall', rows(2) cols(2) scheme(s1mono) iscale(0.75) xsize(9) ysize(6)
graph export "`root'/summary_graphs/overall_pctssi_altst.png", replace hei(1200)

graph combine `grph_combine_nottrained', rows(2) cols(2) scheme(s1mono) iscale(0.75) xsize(9) ysize(6)
graph export "`root'/summary_graphs/nottrained_pctssi_altst.png", replace hei(1200)